<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <!-- 搜索表单 -->
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="会员ID" prop="vipId">
            <el-input
              v-model="queryParams.vipId"
              placeholder="请输入会员ID"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="会员姓名" prop="vipName">
            <el-input
              v-model="queryParams.vipName"
              placeholder="请输入会员姓名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="会员电话" prop="vipPhone">
            <el-input
              v-model="queryParams.vipPhone"
              placeholder="请输入会员电话"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="会员性别" prop="vipGender">
          <el-select v-model="queryParams.vipGender" placeholder="请选择">
            <el-option
              v-for="item in vipGenderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <!-- 数据操作按钮 -->
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5" >
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              :disabled="single"
              @click="handleUpdate"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger"  icon="el-icon-delete" size="small" :disabled="multiple" @click="handleDelete">删除</el-button>
          </el-col>
        </el-row>

        <!-- 数据展示Table -->
        <el-table
          v-loading="loading"
          :data="dataList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="会员ID" align="center" prop="vipId" />
          <el-table-column label="会员姓名" align="center" prop="vipName" />
          <el-table-column label="会员电话" align="center" prop="vipPhone" />
          <el-table-column label="会员性别" align="center" prop="vipGender" />

          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="small"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页加载 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-col>
    </el-row>

    <!-- 新增/修改对话框 默认不显示 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="会员ID" prop="vipId">
          <el-input v-model="form.vipId" placeholder="请输入会员ID" />
        </el-form-item> -->
        <el-form-item label="会员姓名" prop="vipName">
          <el-input v-model="form.vipName" placeholder="请输入会员姓名" />
        </el-form-item>
        <el-form-item label="会员电话" prop="vipPhone">
          <el-input v-model="form.vipPhone" placeholder="请输入员工电话" />
        </el-form-item>
        
        <el-form-item label="会员性别">
          <el-select v-model="form.vipGender" placeholder="请选择">
            <el-option
              v-for="item in vipGenderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listAllVip,
  listVipByPage,
  delVip,
  getVip,
  addVip,
  updateVip,
} from '@/api/system/vip'
// import { filter } from "vue/types/umd";
export default {
  name: "Vip",
  // 定义属性
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: false,
      // 勾选1个或多个
      multiple: true,
      // 遮罩层
      loading: false,
      // 表格数据
      dataList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        vipId: null,
        vipName: null,
        vipGender: null,
      },
      vipGenderOptions: [
        {
          value: "男",
          label: "男",
        },
        {
          value: "女",
          label: "女",
        },
      ],
      // 下拉框选项数据
      //   jobOptions: [
      //     {
      //       employeeLabel: "发型师",
      //       employeeValue: "发型师",
      //     },
      //     {
      //       employeeLabel: "技师",
      //       employeeValue: "技师",
      //     },
      //   ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单
      form: {},
      // 表单校验
      rules: {
        vipid: [{ required: true, message: "会员ID不能为空", trigger: "blur" }],
        vipName: [
          { required: true, message: "会员姓名不能为空", trigger: "blur" },
        ],
        vipPhone:[{ pattern: /^[0-9]{11}$/, message: '请输入正确的号码格式', trigger: 'blur' }]
      },
    };
  },
  // 计算属性，可选
  computed: {
    // filteredEmployee(){
    //   // return this.jobOptions.filter(item => item.jobTitle === ('技师' ||"发型师"));
    // },
  },
  // 监控data变化，可选
  watch: {},
  // 生命周期-创建完成，可选
  created() {
    this.getList();
    this.getVipGenderOptions();
  },
  // 生命周期-挂载完成，可选
  mounted() {
    this.single = true;
  },

  // 方法集合
  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      // 结果赋值this.dataList
      listVipByPage(this.queryParams).then((response) => {
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    /** 查询下拉列表选型 */
    getVipGenderOptions() {
      // 访问后端接口，查外键所有对象
      // 结果赋值this.categoryOptions
      listAllVip().then((response) => {
        // this.vipGenderOptions = response.data;
      });
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.vipId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vipId = row.vipId || this.ids
      console.log(vipId)

      this.$confirm('是否确认删除ID为"' + vipId + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delVip(vipId);
        })
        .then(() => {
          this.getList(); // 刷新数据
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
    /** 表单初始化 */
    resetForm() {
      // 可进行数据初始化工作
      this.form = {
        vipId: null,
        vipName: null,
        vipPhone: null,
      //   eaccount: null,
      //   hireDate: null,
        vipGender: null,
      //   isEmployed: null,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm();
      this.open = true;
      this.title = "添加数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm();
      const vipId = row.vipId || this.ids
      getVip(vipId).then((response) => {
        this.form = response.data
        this.open = true;
        this.title = "修改数据";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.vipId != null) {
            updateVip(this.form).then((response) => {
              console.log(response.data);
              this.$message.success(response.msg);
              this.getList();
              console.log(this.dataList);
              this.cancel();
            });
          } else {
              addVip(this.form).then((response) => {
              this.$message.success(response.msg);
              this.getList();
              this.cancel();
            });
          }
        }else {
          console.log('1111');
        }
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.title = "";
      this.resetForm();
    },
    //在职状况格式化
  //   formatIsEmployed(isEmployed) {
  //     switch (isEmployed) {
  //       case 0:
  //         return "离职";
  //       case 1:
  //         return "在职";
  //       default:
  //         return "";
  //     }
  //   },
    //日期格式化
  //   formatDate(row, column) {
  //     // 获取单元格数据
  //     let data = row[column.property];
  //     if (data == null) {
  //       return null;
  //     }
  //     let dt = new Date(data);
  //     return dt.getFullYear() + "-" + (dt.getMonth() + 1) + "-" + dt.getDate();
  //   },
  },
};
</script>
  
  <style scoped lang="scss">
</style>